<template>
  <div>
    <el-tabs v-model="activeTab" @tab-click="handleTabClick">
      <el-tab-pane label="待支付" name="pending-payment"></el-tab-pane>
      <el-tab-pane label="未发货" name="not-shipped"></el-tab-pane>
      <el-tab-pane label="已发货" name="shipped"></el-tab-pane>
      <el-tab-pane label="待评价" name="pending-review"></el-tab-pane>
      <el-tab-pane label="已完成" name="complete"></el-tab-pane>
    </el-tabs>

    <div class="order-content">
      <h2>{{ activeTabLabel }}</h2>
      <p>您有{{ activeTabLabel }}的订单。</p>
      <div v-if="activeTab === 'pending-payment'">
        <div v-if="pendingOrders.length === 0" class="no-orders">没有待支付的订单。</div>
        <div v-else>
          <ul class="order-list">
            <li v-for="order in pendingOrders" :key="order.id" class="order-item">
              <div class="order-header">
                <span class="order-id">订单号: {{ order.id }}</span>
                <span class="order-total">总价: {{ order.total_price }} 元</span>
              </div>
              <ul class="item-list">
                <li v-for="item in productsList" :key="item.id" class="item"
                  v-if="order.order_items.some(orderItem => orderItem.product_id === item.id)">
                  <span class="item-name">商品名称: {{ item.shop_name }}</span>
                  <span class="item-quantity">数量: {{ order.order_items.find(orderItem => orderItem.product_id ===
                    item.id).quantity }}</span>
                  <span class="item-price">单价: {{ item.shop_price }} 元</span>
                  <img :src="getImageUrl(item.shop_img)" alt="商品图片" class="item-image" /> <!-- 渲染图片 -->
                  <el-button type="text" @click="selectProduct(item.id)">查看详情</el-button>
                </li>
              </ul>
              <el-button type="primary" @click="payOrder(order.id)" class="pay-button">支付订单</el-button>
            </li>
          </ul>
        </div>
      </div>
      <div v-if="activeTab === 'not-shipped'">
        <!-- 未发货订单内容 -->
      </div>
      <div v-if="activeTab === 'shipped'">
        <!-- 已发货订单内容 -->
      </div>
      <div v-if="activeTab === 'pending-review'">
        <!-- 待评价订单内容 -->
      </div>
      <div v-if="activeTab === 'complete'">
        <!-- 已完成订单内容 -->
      </div>
    </div>

    <div v-if="selectedProduct" class="product-details">
      <h3>商品详情</h3>
      <img :src="getImageUrl(selectedProduct.shop_img)" alt="商品图片" class="product-image" />
      <p>商品名称: {{ selectedProduct.shop_name }}</p>
      <p>商品价格: {{ selectedProduct.shop_price }} 元</p>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      activeTab: 'pending-payment',
      pendingOrders: [],
      selectedProduct: null,
      productsList: []
    };
  },
  computed: {
    activeTabLabel() {
      switch (this.activeTab) {
        case 'pending-payment':
          return '待支付';
        case 'not-shipped':
          return '未发货';
        case 'shipped':
          return '已发货';
        case 'pending-review':
          return '待评价';
        case 'complete':
          return '已完成';
        default:
          return '';
      }
    }
  },
  methods: {
    handleTabClick(tab) {
      console.log('当前选中的标签:', tab.name);
      if (tab.name === 'pending-payment') {
        this.fetchPendingOrders();
      }
    },
    fetchPendingOrders() {
      const customer_id = localStorage.getItem('userId');
      if (!customer_id) {
        this.$message.error('用户未登录或ID不存在');
        return;
      }

      axios.get('/orders', {
        params: {
          customer_id: customer_id,
        }
      })
        .then(response => {
          if (response.data.code === 200) {
            this.pendingOrders = response.data.data;
            this.fetchProductDetails();
          } else {
            this.$message.error('获取待支付订单失败: ' + response.data.msg);
          }
        })
        .catch(error => {
          console.error('获取待支付订单失败:', error);
          this.$message.error('获取待支付订单失败，请重试。');
        });
    },
    fetchProductDetails() {
      const productIds = this.pendingOrders.flatMap(order => order.order_items.map(item => item.product_id));
      const uniqueProductIds = [...new Set(productIds)];

      axios.get('/GetProductsList', {

      })
        .then(response => {
          if (response.data.code === 200) {
            this.productsList = response.data.data;
          } else {
            console.error('获取商店数据失败:', response.data.msg);
          }
        })
        .catch(error => {
          console.error('获取商店数据失败:', error);
        });
    },
    getImageUrl(base64String) {
      return `data:image/jpeg;base64,${base64String}`; // 将二进制数据转换为可用的图片 URL
    },
    selectProduct(productId) {
      this.selectedProduct = this.productsList.find(product => product.id === productId);
    },
    payOrder(orderId) {
      console.log('支付订单:', orderId);
      this.$message.success(`订单 ${orderId} 正在支付...`);
    }
  },
  mounted() {
    this.fetchPendingOrders();
  }
};
</script>

<style scoped>
.order-content {
  margin-top: 20px;
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h2 {
  font-size: 24px;
  margin-bottom: 10px;
}

.order-list {
  list-style-type: none;
  padding: 0;
}

.order-item {
  background-color: #fff;
  margin-bottom: 15px;
  padding: 15px;
  border-radius: 5px;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
}

.order-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.order-id {
  font-weight: bold;
}

.order-total {
  color: #ff5722;
  /* 价格颜色 */
}

.item-list {
  list-style-type: none;
  padding: 0;
}

.item {
  display: flex;
  justify-content: space-between;
  padding: 5px 0;
  border-bottom: 1px solid #e0e0e0;
}

.item-name {
  flex: 1;
}

.item-quantity,
.item-price {
  margin-left: 10px;
}

.item-image {
  max-width: 50px;
  /* 设置图片宽度 */
  height: auto;
  margin-left: 10px;
  /* 图片与文本之间的间距 */
}

.no-orders {
  color: #999;
  font-style: italic;
}

.pay-button {
  margin-top: 10px;
}

.product-details {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
}

.product-image {
  max-width: 100%;
  height: auto;
  border-radius: 5px;
}
</style>
